// 获取当前浏览器（用于CFCA）
export const getBrowseInfo = () => {
	var res = {
		name: "",
		version: "",
	};
	var reg;
	var userAgent = self.navigator.userAgent;

	if ((reg = /edge\/([\d\.]+)/i.exec(userAgent))) {
		res.name = "Edge";
		res.version = reg[1];
	} else if ((reg = /edg\/([\d\.]+)/i.exec(userAgent))) {
		res.name = "Edge(Chromium)";
		res.version = reg[1];
	} else if (/msie/i.test(userAgent)) {
		res.name = "Internet Explorer";
		res.version = /msie ([\d\.]+)/i.exec(userAgent)[1];
	} else if (/Trident/i.test(userAgent)) {
		res.name = "Internet Explorer";
		res.version = /rv:([\d\.]+)/i.exec(userAgent)[1];
	} else if (/chrome/i.test(userAgent)) {
		res.name = "Chrome";
		res.version = /chrome\/([\d\.]+)/i.exec(userAgent)[1];
	} else if (/safari/i.test(userAgent)) {
		res.name = "Safari";
		res.version = /version\/([\d\.]+)/i.exec(userAgent)[1];
	} else if (/firefox/i.test(userAgent)) {
		res.name = "Firefox";
		res.version = /firefox\/([\d\.]+)/i.exec(userAgent)[1];
	}
	return res;
};


// version1 == version2 0
// version1 < version2	 -1
// version1 > version2	 1
export function compareVersion(version1, version2) {
	var v1Arr = version1.split(".");
	var v2Arr = version2.split(".");
	var len = Math.min(v1Arr.length, v2Arr.length);
	for (var i = 0; i < len; i++) {
		if (parseInt(v1Arr[i], 10) > parseInt(v2Arr[i], 10)) {
			return 1;
		}

		if (parseInt(v1Arr[i], 10) < parseInt(v2Arr[i], 10)) {
			return -1;
		}
	}

	if (len < v1Arr.length) {
		for (var j = len; j < v1Arr.length; j++) {
			if (parseInt(v1Arr[j], 10) != 0) return 1;
		}
	}

	if (len < v2Arr.length) {
		for (var j = len; j < v2Arr.length; j++) {
			if (parseInt(v2Arr[j], 10) != 0) return -1;
		}
	}

	return 0;
}

// version1 == version2 0
// version1 < version2	 -1
// version1 > version2	 1
export function compareExtensionVersion(version) {
	var browser = getBrowseInfo();
	if ("Edge" === browser.name) {
		return compareVersion(version, "3.4.0.1");
	} else if ("Chrome" === browser.name) {
		return compareVersion(version, "3.4.0.1");
	} else if ("Firefox" === browser.name) {
		return compareVersion(version, "3.4.0.1");
	}
}
